package app

import (
	"bee/pkg/e"
	"bee/pkg/logging"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"net/http"
)

// BindAndValid binds and validates data
func BindAndValid(c *gin.Context, form interface{}, traceId string) (int, int) {

	err := c.Bind(form)
	if err != nil {
		return http.StatusBadRequest, e.InvalidParams
	}
	logging.ZapLogger.Infow("【INBOUND】",  "请求参数", form, "traceId", traceId)

	valid := validation.Validation{}
	check, validErr := valid.Valid(form)
	if validErr != nil {
		return http.StatusInternalServerError, e.ERROR
	}
	if !check {
		MarkErrors(valid.Errors, traceId)
		return http.StatusBadRequest, e.InvalidParams
	}

	return http.StatusOK, e.SUCCESS
}